package com.etalk.crm.serviceImpl;

import com.etalk.crm.dao.TeacherInfoMapper;
import com.etalk.crm.pojo.ClassScheduleInfoMap;
import com.etalk.crm.pojo.TeaComplaintResultMap;
import com.etalk.crm.pojo.TeacherInfoListMap;
import com.etalk.crm.pojo.TeacherInfoResultMap;
import com.etalk.crm.service.TeacherInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jordan
 */
@Service
public class TeacherInfoServiceImpl implements TeacherInfoService {

    @Resource
    private TeacherInfoMapper teacherInfoMapper;
    @Resource
    private HttpSession session;

    /**
     * 查询老师信息列表
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录数
     * @return
     */
    @Override
    public PageInfo<TeacherInfoListMap> searchTeacherInfoList(TeacherInfoListMap  record, Integer pageNum, Integer pageSize){
        if (pageNum==null || pageNum<=0){
            pageNum=1;
        }
        if (pageSize==null || pageSize<10){
            pageSize=15;
        }
        if (record!=null){
            if (StringUtils.isNotEmpty(record.getSearchKey())){
                String[] arrayKey=record.getSearchKey().trim().split(" ");
                record.setListSearchKey(new ArrayList<>());
                for (String key : arrayKey) {
                    if (StringUtils.isNotEmpty(key)){
                        record.getListSearchKey().add(key);
                        record.setSearchKey(record.getSearchKey()+" "+key);
                    }
                }
                record.setSearchKey(record.getSearchKey().trim());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<TeacherInfoListMap> list=teacherInfoMapper.selectTeacherInfoList(record);
        return new PageInfo<>(list);
    }

    /**
     * 查询第一常用老师名下设置自动约课学员
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录数
     * @return
     */
    @Override
    public PageInfo<TeacherInfoResultMap> searchFistTeaAutoBookingList(TeacherInfoResultMap record, Integer pageNum, Integer pageSize){
        if (pageNum==null || pageNum<=0){
            pageNum=1;
        }
        if (pageSize==null || pageSize<10){
            pageSize=100;
        }
        if (record!=null){
            if (StringUtils.isNotEmpty(record.getSearchKey())){
                String[] arrayKey=record.getSearchKey().trim().split(" ");
                record.setListSearchKey(new ArrayList<>());
                for (String key : arrayKey) {
                   if (StringUtils.isNotEmpty(key)){
                       record.getListSearchKey().add(key);
                       record.setSearchKey(record.getSearchKey()+" "+key);
                   }
                }
                record.setSearchKey(record.getSearchKey().trim());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<TeacherInfoResultMap> list=teacherInfoMapper.selectFirstTeaAutoBookingList(record);
        if (list!=null && list.size() > 0){
            int tempTeacherId=0,rowSpan=0;
            TeacherInfoResultMap tempTeaInfo=null;
            for (TeacherInfoResultMap result : list) {
                if (tempTeacherId!= result.getTeacher1Id()){
                    if (tempTeacherId>0){
                        tempTeaInfo.setRowSpan(rowSpan);
                        rowSpan=0;
                    }
                    tempTeacherId= result.getTeacher1Id();
                    tempTeaInfo=result;
                }
                rowSpan+=1;
            }
            if (tempTeaInfo != null) {
                tempTeaInfo.setRowSpan(rowSpan);
            }
        }
        return new PageInfo<>(list);
    }

    /**
     * 查询离职老师名下学员列表
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录数
     * @return
     */
    @Override
    public PageInfo<TeacherInfoResultMap> searchResignTeacherStudentList(TeacherInfoResultMap record, Integer pageNum, Integer pageSize){
        if (pageNum==null || pageNum<=0){
            pageNum=1;
        }
        if (pageSize==null || pageSize<10){
            pageSize=100;
        }
        if (record!=null){
            if (StringUtils.isNotEmpty(record.getSearchKey())){
                String[] arrayKey=record.getSearchKey().trim().split(" ");
                record.setListSearchKey(new ArrayList<>());
                for (String key : arrayKey) {
                    if (StringUtils.isNotEmpty(key)){
                        record.getListSearchKey().add(key);
                        record.setSearchKey(record.getSearchKey()+" "+key);
                    }
                }
                record.setSearchKey(record.getSearchKey().trim());
            }
        }
        int roleId=Integer.parseInt(session.getAttribute("roleId").toString());
        if (roleId==8 || roleId==9){
            if (record==null){
                record=new TeacherInfoResultMap();
            }
            if (record.getListSscId()==null) {
                List<Integer> list=new ArrayList<>(1);
                list.add(Integer.parseInt(session.getAttribute("userid").toString()));
                record.setListSscId(list);
            }
        }

        PageHelper.startPage(pageNum, pageSize);
        List<TeacherInfoResultMap> list=teacherInfoMapper.selectResignTeacherStudentList(record);
        return new PageInfo<>(list);
    }

    /**
     * 老师投诉学员记录
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录
     * @return
     */
    @Override
    public PageInfo<TeaComplaintResultMap> searchTeaComplaintListByPage(TeaComplaintResultMap record, Integer pageNum, Integer pageSize){
        if (pageNum==null || pageNum<=0){
            pageNum=1;
        }
        if (pageSize==null || pageSize<10){
            pageSize=25;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<TeaComplaintResultMap> list=teacherInfoMapper.selectTeacherComplaintList(record);
        return new PageInfo<>(list);
    }

    /**
     * 查询老师课程排班信息展示
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录
     * @return
     */
    @Override
    public PageInfo<ClassScheduleInfoMap> searchClassScheduleInfoListByPage(ClassScheduleInfoMap record, Integer pageNum, Integer pageSize){
        if (pageNum==null || pageNum<=0){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=50;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<ClassScheduleInfoMap> list=teacherInfoMapper.selectClassScheduleInfoList(record);
        return new PageInfo<>(list);
    }
}
